# -*- coding: utf-8 -*-
# 新维大健康空间站的机器人
# 接收公众号的对话消息
import re

from flask import request

#from app.util.redis_func import con_redis

_author_ = 'yykrlc'
#import copy

import io
import os
from app.werobot import WeRoBot
from app.wxgzh import weixin
from app.werobot.contrib.flask import make_view
from app.app_config import BaseConfig
#from app.wxgzh.service import message_save


# robot_1 = WeRoBot(token=BaseConfig.open_token, enable_pretty_logging=False)
robot_1 = WeRoBot(token=BaseConfig.open_token)
robot_1.config['SESSION_STORAGE'] = False  # 不用 werobot 自带的储存功能  默认 SQLiteStorage
robot_1.config["APP_ID"] = BaseConfig.component_appid
robot_1.config["APP_SECRET"] = BaseConfig.component_appsecret
robot_1.config["ENCODING_AES_KEY"] = BaseConfig.encodingAESKey

open_token = BaseConfig.open_token
component_appid = BaseConfig.component_appid
encodingAESKey = BaseConfig.encodingAESKey


# ---替换机器人默认错误页面
# def make_error_page(url):
#     with io.open(os.path.join(os.path.dirname(__file__)+'/..', 'templates/error.html'), 'r', encoding='utf-8') as error_page:
#         return error_page.read().replace('{url}', url)
#robot_1.make_error_page = make_error_page
#socketio_sid_list = {'sid1':''}
# --------------------

'''
robot.text	文本 (Message)
robot.image	图像 (Message)
robot.location	位置 (Message)
robot.link	链接 (Message)
robot.voice	语音 (Message)
robot.unknown	未知类型 (Message)
robot.subscribe	被关注 (Event)
robot.unsubscribe	被取消关注 (Event)
robot.click	自定义菜单事件 (Event)
robot.view	链接 (Event)
robot.scancode_push	扫描推送 (Event)
robot.scancode_waitmsg	扫描弹消息 (Event)
robot.location_event	上报位置 (Event)
robot.unknown_event	未知类型 (Event)
'''
def re_num(message):
    matchs = re.findall(u'分组\D{0,3}(\d+).*', message.content)
    if len(matchs) > 0:
        return matchs[0]

    matchs = re.findall(u'分组\s*(一|二|三|四|五|六|七|八|九|十|十一|十二)\s*', message.content)
    if len(matchs) > 0:
        zh_num_dict = {
            u"一": 1,
            u"二": 2,
            u"三": 3,
            u"四": 4,
            u"五": 5,
            u"六": 6,
            u"七": 7,
            u"八": 8,
            u"九": 9,
            u"十": 10,
            u"十一": 11,
            u"十二": 12
        }
        return zh_num_dict[matchs[0]]

    if re.compile(r'^\d{1,2}$').match(message.content) is not None:
        if int(message.content) > 0:
            return message.content

@robot_1.text
def robot_text(message):
    print '--message--', message
    # for i in dir(message):
    #     print i, '---', eval('message.%s' % i)
    # print '--message.source,--', message.source
    print '------------request.args-----------'
    print request.args
    print '------------request.data-----------'
    print request.data
    #message_back = message_save.save_text_message(message, robot_1)
    target = message.target
    source = message.source
    print message
    print target
    print source
    
    return u'收到'
 

@robot_1.image
def robot_image(message):
    print '--message--', message
    return u'您的图片地址:' + message.img


@robot_1.voice  # 语音
def robot_voice(message):
    print '--message--', message
    for i in dir(message):
        print i, '---', eval('message.%s' % i)
    print message.source, '-------',message.media_id
    #xx = robot.client.send_voice_message(message.source, message.media_id)
    #print '---xx---',xx
    return u'暂不支持语音'


@robot_1.unknown  # 位置类型,用来接收此路由的其他信息
def robot_unknown(message):
    print '---### robot_unknown_message ###---',message
    
    return u'未知信息'


@robot_1.error_page  # 错误类型,用来接收未通过验证的其他信息
def robot_error_page(url):
    print '---### robot_error_page ###---', url
    return 'success'
    
    
# -------------------

weixin.add_url_rule(rule='/appid/callback', endpoint='werobot', view_func=make_view(robot_1), methods=['GET', 'POST'])


